package com.bjdd.jdy.ui.inspection;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.bjdd.jdy.Constant;
import com.bjdd.jdy.R;
import com.bjdd.jdy.adapter.ImageAdapter;
import com.bjdd.jdy.base.BaseActivity;
import com.bjdd.jdy.base.RxManager;
import com.bjdd.jdy.ui.previewImg.PreviewImgActivity;
import com.bjdd.jdy.util.ImageUtil;
import com.bjdd.jdy.util.L;
import com.bjdd.jdy.util.StatusBarUtil;
import com.bjdd.jdy.util.T;
import com.bjdd.jdy.view.widget.ZoomImage;
import com.liji.imagezoom.util.ImageZoom;
import com.liuguangqiang.ipicker.IPicker;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class InspectionActivity extends BaseActivity<InspectionPresenter, InspectionModel> implements InspectionContract.View {


    @Bind(R.id.toolbar_back)
    ImageView toolbarBack;
    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.tv_toolbar_right)
    TextView tvToolbarRight;
    @Bind(R.id.gv_takephoto)
    GridView gvTakephoto;
    private ImageAdapter adapter;
    private List<String> paths;
    private int MAX = 8;
    private int position = -1;
    private String orderId;
    private ImageUtil imageUtil;
    @Override
    public int getLayoutId() {
        return R.layout.activity_inspection;
    }

    @Override
    public void initView() {
        StatusBarUtil.setColor(this, Color.parseColor("#F9F9F9"),0);
        title.setText("拍照验货");
        tvToolbarRight.setText("上传");
        tvToolbarRight.setVisibility(View.VISIBLE);
        Bundle bundle = getIntent().getExtras();
        orderId = bundle.getString("orderId");
        paths = new ArrayList<>();
        paths.add(Constant.DEFAULT_PHOTO);
        adapter = new ImageAdapter(context,paths,8);
        gvTakephoto.setAdapter(adapter);
        gvTakephoto.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int p, long id) {
                String path =  (String)adapter.getItem(p);
                L.d("点击项："+p);
                if (path.equals(Constant.DEFAULT_PHOTO)){
                    int limit = MAX - paths.size() + 1;
                    L.d("可选择数量:"+limit);
                    IPicker.setLimit(limit);
                    position = -1;
                    IPicker.open(context);
                }else {
                    position = p;
                    List<String> urls = new ArrayList<String>();
                    for (int i=0;i<paths.size();i++){
                        if (!Constant.DEFAULT_PHOTO.equals(paths.get(i))){
                            urls.add(paths.get(i));
                        }
                    }
                    ZoomImage.Builder builder = new ZoomImage.Builder(context);
                    builder.setMode(ZoomImage.LOCAL_MODE)
                            .setData(urls)
                            .setIndex(position)
                            .setEventName(Constant.DEL_IMG)
                            .show();
                }
            }
        });
        IPicker.setOnSelectedListener(new IPicker.OnSelectedListener() {
            @Override
            public void onSelected(List<String> list) {
                if (list == null || list.size() <= 0)
                    return;
                if (position != -1){
                    paths.set(position,list.get(0));
                    adapter.notifyDataSetChanged();
                    return;
                }
                if (paths.get(paths.size()-1).equals(Constant.DEFAULT_PHOTO)){
                    paths.remove(paths.size() - 1);
                }
                for (int i=0;i<list.size();i++){
                    paths.add(list.get(i));
                }
                if (paths.size() < MAX){
                    paths.add(Constant.DEFAULT_PHOTO);
                }
                adapter.notifyDataSetChanged();
            }
        });
        imageUtil = new ImageUtil(context);
    }


    @OnClick({R.id.toolbar_back,R.id.tv_toolbar_right})
    public void onViewClicked(View view) {
        switch (view.getId()){
            case R.id.tv_toolbar_right:
                if (paths.size() <= 1){
                    T.show("请先选择图片");
                    return;
                }
                if (paths.size() < 3){
                    T.show("至少上传两张图片");
                    return;
                }
                /*Map<String, RequestBody> params = new HashMap<>();
                for (int i=0;i<paths.size();i++){
                    if (!paths.get(i).equals(Constant.DEFAULT_PHOTO)){
                        File file = new File(paths.get(i));
                        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"),file);
                        params.put("photos\";filename=\""+i+"+image.png\"" ,requestBody);
                    }
                }
                L.d("订单号:"+orderId);
                presenter.upLoadImg(orderId,params);*/
                paths.remove(paths.size()-1);
                presenter.startCompress(orderId,paths);
                break;
            case R.id.toolbar_back:
                finish();
                break;
        }
    }

    @Override
    public void upLoadBack() {
        T.show("上传成功");
        RxManager rxManager = new RxManager();
        rxManager.post(Constant.INSPECTION_FINISH,"");
        finish();
    }

    @Override
    public void delImg(int position) {
        paths.remove(position);
        adapter.notifyDataSetChanged();
    }

}
